<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Advertising_entity{
	var $id = 0;
	var $type = '';
	var $url = '';
	var $position = '';
	var $click = 0;
	var $start_time = 0;
	var $end_time = 0;
	var $status = '';
	var $created_date = 0;
	var $ordering = 0;
	var $image = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_ADVERTISING)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->type = $result[0]->type;
			$this->url = $result[0]->url;
			$this->position = $result[0]->position;
			$this->click = $result[0]->click;
			$this->start_time = $result[0]->start_time;
			$this->end_time = $result[0]->end_time;
			$this->status = $result[0]->status;
			$this->created_date = $result[0]->created_date;
			$this->ordering = $result[0]->ordering;
			$this->image = $result[0]->image;
			
			// Create user folder if not exists
			$directory = "./uploads/default/advertising";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			return $this;
		}
		return false;
	}
	
	function setType($input){ $this->type=$input; }
	function setUrl($input){ $this->url=$input;}
	function setPosition($input){ $this->position=$input; }
	function setClick($input){ $this->click=$input; }
	function setStartTime($input){ $this->start_time=$input; }
	function setEndTime($input){ $this->end_time=$input; }
	function setStatus($input){ $this->status=$input; }
	function setCreatedDate($input){ $this->created_date=$input; }
	function setOrdering($input){ $this->ordering=$input; }
	function setImage($input){ $this->image=$input; }
	
	function getId(){ return $this->id; }
	function getType(){ return $this->type; }
	function getUrl(){ return $this->url; }
	function getPosition(){ return $this->position; }
	function getClick(){ return $this->click; }
	function getStartTime(){ return $this->start_time; }
	function getEndTime(){ return $this->end_time; }
	function getStatus(){ return $this->status; }
	function getCreatedDate(){ return $this->created_date; }
	function getOrdering(){ return $this->ordering; }
	function getImage(){ return $this->image; }
	
	function getAdvFileDir(){
		return "./uploads/default/advertising/";
	}
	
	function getAdvImage(){
		return $this->getAdvFileDir().$this->getImage();
	}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('type', $this->type);
		$ci->db->set('url', $this->url);
		$ci->db->set('position', $this->position);
		$ci->db->set('click', $this->click);
		$ci->db->set('start_time', $this->start_time);
		$ci->db->set('end_time', $this->end_time);
		$ci->db->set('status', $this->status);
		$ci->db->set('created_date', $this->created_date);
		$ci->db->set('ordering', $this->ordering);
		$ci->db->set('image', $this->image);
		
		$ci->db->where('id', $this->id)->update(TBL_ADVERTISING);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		$ci->db->set('status', 'show');
		$ci->db->set('created_date', time());
		$ci->db->insert(TBL_ADVERTISING);
		
		return $ci->db->insert_id();
	}
	
	function getListAdvertising(){
		$ci=& get_instance();
		return $ci->db->get(TBL_ADVERTISING)->result();
	}
	
	function getListAdvByPosition($pos= 'left'){
		$ci=& get_instance();
		return $ci->db->where(array('status'=>'show','position'=>$pos))->order_by('ordering','asc')->get(TBL_ADVERTISING)->result();
	}
	//end class
}
